trigger Kreditor_Invoice_Status_BIBU on Kreditor_Invoice_Status__c (before insert, before update) {
	
	id bankTransferId = [select id from Chart_of_Accounts__c where Account_Type__c = 'Finanzen' and Account_Description__c = 'Bank'].id;
	id directDebitId = [select id from Chart_of_Accounts__c where Account_Type__c = 'Finanzen' and Account_Description__c = 'Bank'].id;
	id creditCard0Id = [select id from Chart_of_Accounts__c where Account_Type__c = 'Finanzen' and Account_Description__c = 'Mastercard xx1000931'].id;
	id creditCard1Id = [select id from Chart_of_Accounts__c where Account_Type__c = 'Finanzen' and Account_Description__c = 'Mastercard x20452043'].id;
	id creditCard2Id = [select id from Chart_of_Accounts__c where Account_Type__c = 'Finanzen' and Account_Description__c = 'Mastercard xx9019208'].id;
	id cashId = [select id from Chart_of_Accounts__c where Account_Type__c = 'Finanzen' and Account_Description__c = 'Kasse'].id;

	list<id> invoiceIds = new list<id>();
	map<id, Kreditor__c> idToInvoice = new map<id, Kreditor__c>();
	
	//setting the ids of the VAT Accounts, getting the ids of all accounts (in order to set Kreditorennummer__c)			
	for(Kreditor_Invoice_Status__c invoiceStatus : trigger.new) {
		invoiceIds.add(invoiceStatus.Kreditor_Invoice__c);
	}
	idToInvoice = new map<id, Kreditor__c>([SELECT Id, VAT_Account_new__c, Kreditorennummer__c FROM Kreditor__c WHERE Id IN: invoiceIds]);
	for(integer i = 0; i < trigger.new.size(); i++) {
		trigger.new[i].VAT_Account__c = idToInvoice.get(trigger.new[i].Kreditor_Invoice__c).VAT_Account_new__c;
		if(idToInvoice.get(trigger.new[i].Kreditor_Invoice__c).Kreditorennummer__c != null) {
			trigger.new[i].Kreditorennummer__c = idToInvoice.get(trigger.new[i].Kreditor_Invoice__c).Kreditorennummer__c;
		} else {
			trigger.new[i].addError('Cannot save Eingangsrechnungsstatus, Eingangsrechnung has no Kreditorennummer');
		}
		if(Trigger.isInsert && trigger.new[i].Payment_Status__c == 'Paid' ||
			Trigger.isUpdate && trigger.new[i].Payment_Status__c == 'Paid') 
		{
			if(trigger.new[i].Paid_new__c == null) {
				trigger.new[i].Paid_new__c = Date.today();
			}
			if(trigger.new[i].Payment_method__c == 'Bank transfer') {trigger.new[i].credit_account__c = bankTransferId;}
			if(trigger.new[i].Payment_method__c == 'Direct debit') {trigger.new[i].credit_account__c = directDebitId;}
			if(trigger.new[i].Payment_method__c == 'Credit Card') {trigger.new[i].credit_account__c = creditCard0Id;}
			if(trigger.new[i].Payment_method__c == 'Credit Card 1') {trigger.new[i].credit_account__c = creditCard1Id;}
			if(trigger.new[i].Payment_method__c == 'Credit Card 2') {trigger.new[i].credit_account__c = creditCard2Id;}
			if(trigger.new[i].Payment_method__c == 'Cash') {trigger.new[i].credit_account__c = cashId;}
			
		}
		if(Trigger.isInsert && trigger.new[i].Payment_Status__c == 'Unpaid' ||
			Trigger.isUpdate && trigger.new[i].Payment_Status__c == 'Unpaid') 
		{
			if(trigger.new[i].Payment_method__c == null) {
				trigger.new[i].credit_account__c = null;
				trigger.new[i].Paid_new__c = null;}
		}
	}
}